package cn.shiro.shiroservice.auth.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.shiro.shiroservice.auth.pojo.dto.TokenManagerDTO;
import cn.shiro.shiroservice.auth.service.TokenManager;
import cn.shiro.shiroservice.auth.utils.JwtUtils;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static cn.shiro.shiroservice.common.constant.ManaPoint.*;

/**
 * &#064;Time 2024 一月 星期一 10:23
 *
 * @author ShangGuan
 */
public class SimpleTokenManager implements TokenManager {

    /**
     * 过期时间
     */
    private Integer expirationTime;
    /**
     * 时间
     */
    public final String TIME_KEY = ":time";


    public SimpleTokenManager(Integer expirationTime) {
        this.expirationTime = expirationTime;
    }


    @Override
    public TokenManagerDTO generatesToken(String userName,String sessionId) {
        if (expirationTime == null) {
            this.expirationTime = ACCOUNT_TOKEN_EXPIRATION_TIME;
        }
        Map<String, Object> claims = new HashMap<>();
        claims.put(TOKEN_ACCOUNT, userName);
        claims.put(TOKEN_SESSION_ID, sessionId);
        String token = JwtUtils.generate(claims, Long.valueOf(expirationTime));
        String dateFormat = DateUtil.format(new Date(new Date().getTime() + expirationTime), DatePattern.NORM_DATETIME_PATTERN);
        return new TokenManagerDTO(token, dateFormat);
    }




}
